let app = getApp()

Page({

  data: {
    phone: '',
    newphone: ''
  },


  //监听页面加载
  onLoad(options) {
    this.setData({
      title: options.title,
      phone: app.globalData.phone
    });
  },

  formSubmit(e){
    let phone = this.data.phone
    let username = app.globalData.userName
    let newphone = e.detail.value.newphone

    console.log(phone)
    console.log(username)
    console.log(newphone)
    if (newphone == '' || newphone == null){
      dd.alert({
        content: "新号码不能为空！ "
      })
    }else if(!(/^1[34578]\d{9}$/.test(newphone))){
      dd.alert({
        content: "新号码格式不对！"
      })
    }else if(phone == newphone){
      dd.alert({
        content: "新号码与旧号码相同，请重新输入新密码"
      })
    }else{
      dd.httpRequest({
        headers: {
          "Content-Type": "application/json"
        },
        url: app.userUrl + '/DingDing/AccountManagement/updateUserByDingDing',
        method: 'POST',
        data: JSON.stringify({
            username: app.globalData.userName,
            phone: newphone
        }),
        dataType: 'json',
        success: (res) => {
          console.log("号码修改成功后的数据：",res)
          if(res.data.result == "success"){
            dd.alert({
              content: '号码修改成功',
              buttonText: '确定',
              success: (res) => {
                app.globalData.phone = newphone
                  dd.reLaunch({
                    url: '/page/mine/mine'
                  })
                }
            })
          }else{
            dd.alert({
              content: '号码修改失败',
              buttonText: '确定'
            })
          }
        },
        fail: (res) => {
          console.log(res)
          dd.alert({
            content: '网络异常',
            button: '确定'
          })
        }
      })
    }

  }





});